from django.db.models import Q
from django.shortcuts import render
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from doctor import models, doctorser
from utils.authentication import JWTAuthenticationss


# Create your views here.

class DoctorListView(APIView):
    def get(self, request):
        departments = models.Department.objects.filter(parent__isnull=False)
        ser = doctorser.DepartmentSerializer(departments, many=True)
        return Response(ser.data)


class DepartmentDetailView(APIView):
    def get(self, request):
        departments = models.Department.objects.filter(parent__isnull=True)
        ser = doctorser.DepartmentDetailSerializer(departments, many=True)
        return Response({"msg": "ok", "departments": ser.data})


class AreasListView(APIView):
    def get(self, request):
        areas = models.Area.objects.filter(parent__isnull=True)
        ser = doctorser.AreaSerializer(areas, many=True)
        return Response({"msg": "ok", "areas": ser.data})


class OrderView(APIView):
    def get(self, request):
        did = request.query_params.get("did")
        order = request.query_params.get("order")
        area = request.query_params.get("area")
        max_price = request.query_params.get("max_price")
        min_price = request.query_params.get("min_price")
        jid = request.query_params.get("jid")

        # 使用Q对象构建查询条件
        conditions = Q()
        if did:
            conditions &= Q(did=did)
        if area:
            conditions &= Q(aid=area)
        if max_price:
            conditions &= Q(price__lte=max_price)
        if min_price:
            conditions &= Q(price__gte=min_price)
        if jid:
            conditions &= Q(jid=jid)

        # 根据order参数排序
        if order and order != "0":
            data = models.doctor.objects.filter(conditions).order_by(order)
        else:
            data = models.doctor.objects.filter(conditions)

        # 序列化数据
        ser = doctorser.DoctorSerializer(data, many=True)

        # 返回响应
        return Response({"msg": "ok", "data": ser.data})


class CommentView(APIView):
    def get(self, request):
        did = request.query_params.get("did")
        data = models.Comment.objects.filter(did=did).order_by("-created_time")
        ser = doctorser.CommentSerializer(data, many=True)
        return Response({"msg": "ok", "data": ser.data})


class PatientView(APIView):
    authentication_classes = [JWTAuthenticationss]

    def get(self, request):
        user = request.query_params.get("user")
        doctor = request.query_params.get("doctor")
        data = models.Patient.objects.filter(user=user, doctor=doctor)
        ser = doctorser.PatientSerializer(data, many=True)
        return Response({"msg": "ok", "data": ser.data})


class SelectedView(APIView):
    authentication_classes = [JWTAuthenticationss]

    def get(self, request):
        id = request.query_params.get("id")
        selected = request.query_params.get("selected")
        print(id)
        print(selected)
        models.Patient.objects.update(selected=False)
        if selected == "false":
            models.Patient.objects.filter(id=id).update(selected=True)
        return Response({"msg": "ok"})


class PatientDetailView(APIView):
    authentication_classes = [JWTAuthenticationss]

    def get(self, request):
        id = request.query_params.get("id")
        data = models.Patient.objects.filter(id=id).first()
        ser = doctorser.PatientSerializer(data)
        return Response({"msg": "ok", "data": ser.data})


class AddPatientView(APIView):
    def post(self, request):
        data = request.data
        print(data)
        ser = doctorser.PatientSerializer(data=data)
        if ser.is_valid():
            ser.save()
            return Response({"msg": "ok"})
        else:
            print(ser.errors)
            return Response({"msg": "error", "error": ser.errors})


class UpdatePatientView(APIView):
    def post(self, request):
        id = request.data.get("id")
        data = request.data
        print(data)
        ser = doctorser.PatientSerializer(instance=models.Patient.objects.get(id=id), data=data)
        if ser.is_valid():
            ser.save()
            return Response({"msg": "ok"})
        else:
            print(ser.errors)
            return Response({"msg": "error", "error": ser.errors})


class UploadViews(APIView):

    def post(self, request):
        file = request.FILES.get('file')
        with open(f"./media/uploads/{file.name}", 'wb') as f:
            for chunks in file.chunks():
                f.write(chunks)
        return Response({"message": "上传成功", "code": "10000"}, status=status.HTTP_200_OK)


class AddQuestionView(APIView):
    def post(self, request):
        content = request.data.get("content")
        image = request.data.get("image")
        if image is None:
            image = ""
        ser = doctorser.QuestionSerializer(data={"content": content, "img": "/upload/uploads/"+image})
        if ser.is_valid():
            question = ser.save()
            return Response({"msg": "ok", "question": question.id})
        else:
            return Response({"msg": "error", "error": ser.errors})
